//
//  CYAccountViewModel.swift
//  CYSinaWB
//
//  Created by mac on 16/2/20.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import  AFNetworking

class CYAccountViewModel: NSObject {
    
    static let sharedViewModel: CYAccountViewModel = CYAccountViewModel()
    //用户账户模型
    var userAccount: CYUserAccount?
    //构造函数私有化
   private override init() {
        super.init()
        userAccount = CYUserAccount.loadUserInfo()
    }
    
    //用户是否登录 只读
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
    //用户的昵称
    var userName: String? {
        return userAccount?.name
    }
    //用户的头像
    var headImgURL: NSURL? {
        if let urlString = userAccount?.avatar_large {
            return NSURL(string: urlString)
        }
        return nil
    }
    
    //当前用户的token计算
    var access_token: String? {
        return userAccount?.access_token
    }
    
     func loadAccessToken(code: String,finished: (isSuccess: Bool) -> ()) {
        //使用AFN发送post请求
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret": client_secret ,"grant_type":"authorization_code","code":code, "redirect_uri":redirect_uri]
        let AFN = AFHTTPSessionManager()
        //插入支持的反序列化格式
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print(result)
            //将anyobject类型转化为字典
            guard let dict = result as? [String: AnyObject] else {
                finished(isSuccess: false)
                return
            }
            //一定是字典数据了
            //            let access_token = dict["access_token"] as! String
            //            let uid = dict["uid"] as! String
            //            print(access_token)
            
            let userAccout = CYUserAccount(dict: dict)
            print(userAccout)
            
            //获取用户信息
            //            self.loadUserInfo(access_token, uid: uid)
            self.loadUserInfo(userAccout, finished: finished)
            
            }) { (_, error) -> Void in
                finished(isSuccess: false)
                print(error)
        }
    }
    //请求用户信息
    private func loadUserInfo(userAccount: CYUserAccount,finished: (isSuccess: Bool) -> ()){
        //    private func loadUserInfo(access_token: String, uid: String) {
        //获取用户信息的借口
        let urlString = "https://api.weibo.com/2/users/show.json"
        //        let parameters = ["access_token":access_token,"uid":uid]
        let parameters = ["access_token":userAccount.access_token!,"uid":userAccount.uid!]
        let AFN = AFHTTPSessionManager()
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print(result)
            
            guard let dict = result as? [String: AnyObject] else {
                finished(isSuccess: false)
                return
            }
            let name = dict["name"] as! String
            let avatar_large = dict["avatar_large"] as! String
            //            print(name,avatar_large)
            //完善模型数据
            userAccount.name = name
            userAccount.avatar_large = avatar_large
            //            print(userAccount)
            
            //保存
            userAccount.saveAccount()
            
            //用户模型赋值
//            CYAccountViewModel.sharedViewModel.userAccount = userAccount
            self.userAccount = userAccount
            
            //保存完信息后做回调
            finished(isSuccess: true)
            
            }) { (_, error) -> Void in
                finished(isSuccess: false)
                print(error)
        }
    }

}
